package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据用户id查询用户购物车
     *
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> queryUserShoppingCart() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .list();
        return R.success(shoppingCarts);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        // 获取当前登录的user_id
        Long userId = BaseContext.getCurrentId();
        // 查询添加的菜是否已在购物车中存在
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId,
                        shoppingCart.getSetmealId())
                .eq(ShoppingCart::getUserId, userId)
                .one();
        if (cart == null) {
            // 新增的在购物车中不存在直接添加
            shoppingCart.setUserId(userId);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
        // 新增的在购物车中已存在更新购物车
        cart.setNumber(cart.getNumber() + 1);
        shoppingCartService.updateById(cart);
        return R.success(cart);
    }

    /**
     * 减少购物车中菜品或套餐数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R<Object> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        // 查询用户购物车中的菜品或套餐
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId,
                        shoppingCart.getSetmealId())
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .one();
        if (cart == null) {
            return R.error("您的购物车中已无菜品和套餐");
        }
        // 购物车中菜品或套餐数量减少后不为0
        if (cart.getNumber() > 1) {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        }
        // 购物车中菜品或套餐数量减少后为0
        cart.setNumber(0);
        shoppingCartService.removeById(cart.getId());
        return R.success(cart);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> cleanShoppingCart() {
        shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .remove();
        return R.success("购车清空成功");
    }
}
